﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace TransTaskWithCompensation
{
	public sealed class StreamUtil
	{
        private StreamUtil()
        {
        }

        public static string GetSerializationStream(object graph)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, graph);
                return Convert.ToBase64String(stream.ToArray());
            }
        }

        public static object DeserializeFromString(string data)
        {
            try
            {
                byte[] array = Convert.FromBase64String(data);
                using (MemoryStream stream = new MemoryStream(array))
                {
                    IFormatter formatter = new BinaryFormatter();
                    return formatter.Deserialize(stream);
                }
            }
            catch
            {
                // do nothing, just ignore any possible errors
            }
            finally
            {

            }
            return null;
        }

	}
}
